package com.hnevc.acountms.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hnevc.acountms.R;
import com.hnevc.acountms.dao.PwdDao;
import com.hnevc.acountms.model.Tb_pwd;

import org.litepal.tablemanager.Connector;

public class LoginActivity extends AppCompatActivity {

    Button mBtnLogin;
    EditText mEtPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        initDataBase();
        mBtnLogin = (Button) findViewById(R.id.id_btn_Login);
        mEtPassword = (EditText) findViewById(R.id.id_et_Login);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = mEtPassword.getText().toString();
                if(validate(password)) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("Login", true);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this, "密码错误！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initDataBase() {
        SQLiteDatabase db = Connector.getDatabase();
        Tb_pwd pwd = new Tb_pwd();
        pwd.setPassword("123");
        pwd.save();
    }

    private boolean validate(String password) {
        Tb_pwd pwd = new PwdDao().find(1);

        if(password .equals(pwd.getPassword())){
            return true;
        }else {
            return false;
        }
    }
}
